package online.lmds.sdmanage.main.device.controller;

import online.lmds.sdmanage.common.Constants;
import online.lmds.sdmanage.common.entity.Page;
import online.lmds.sdmanage.main.base.BaseContoller;
import online.lmds.sdmanage.main.device.entity.Device;
import online.lmds.sdmanage.main.device.entity.DeviceLog;
import online.lmds.sdmanage.main.device.service.DeviceLogService;
import online.lmds.sdmanage.main.device.service.DeviceService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Brighten Ma on 2017/4/9.
 * 设备
 */


@Controller
@RequestMapping("/device")
public class DeviceController  extends BaseContoller{

    @Resource(name = "DeviceService")
    private DeviceService deviceService;

    @Resource(name = "DeviceLogService")
    private DeviceLogService deviceLogService;



    @RequestMapping(value = "/test")
    @ResponseBody
    public Object getTestDevice(){
        Map<String, String> result = new HashMap<>();

        result.put("test1","hello ring");

        return result;
    }



    @RequestMapping(value = "/save")
    @ResponseBody
    public Object save(Device device){
        setUserInfo(device);
        return deviceService.save(device);
    }

    @RequestMapping(value = "/delet")
    @ResponseBody
    public Object delet(Device device){
        setUserInfo(device);
        return deviceService.del(device);
    }


    @RequestMapping(value = "/listByUser")
    @ResponseBody
    public Object listByUser( Device device,Integer pageNumber, Integer pageSize){
        if (pageSize == null || pageSize < 1) {
            pageSize = Constants.DEFULT_PAGE_SIZE;
        }
        Page page = new Page(pageSize);
        page.setPageNumber(pageNumber);
        setUserInfo(device);
        return deviceService.listByUserId(device, page);
    }


    @RequestMapping(value = "/list/log")
    @ResponseBody
    public Object lisLog(DeviceLog log, Integer pageNumber, Integer pageSize){
        setUserInfo(log);
        if (pageSize == null || pageSize < 1) {
            pageSize = Constants.DEFULT_PAGE_SIZE;
        }
        Page page = new Page(pageSize);
        page.setPageNumber(pageNumber);

        return deviceLogService.list(log, page);
    }

}
